package com.yzy.mall.repository

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.jeremyliao.liveeventbus.LiveEventBus
import com.yzy.mall.component.comm.subscribeLifecycle


object UpdateGlobal {

    private const val TAG_SEND_ADDRESS = "tag_send_update"

    fun addObserve(
        lifecycleOwner: LifecycleOwner,
        messageCallBack: (isSuccess: Boolean) -> Unit
    ) {
        val observer = Observer<Boolean> {
            it?.let { isSuccess ->
                messageCallBack.invoke(isSuccess)
            }
        }
        subscribeLifecycle(lifecycleOwner) {
            onCreate = {
                LiveEventBus
                    .get(TAG_SEND_ADDRESS, Boolean::class.java)
                    .observeForever(observer)
            }
            onDestroy = {
                LiveEventBus
                    .get(TAG_SEND_ADDRESS, Boolean::class.java)
                    .removeObserver(observer)
            }
        }
    }

    fun setSend(isSuccess: Boolean) {
        LiveEventBus
            .get(TAG_SEND_ADDRESS, Boolean::class.java)
            .post(isSuccess)
    }
}
